<?php
/**
 * created by zzy
 * date: 2018/1/11 10:42
 */

namespace App\Http\Admin\Export\Controllers;
use App\Eloquent\Ygt\Process;
use App\Eloquent\Ygt\ProcessField;
use App\Eloquent\Ygt\ProcessFieldCategory;
use App\Eloquent\Ygt\ProcessFieldCompany;
use App\Eloquent\Ygt\ProcessFieldRelation;
use App\Eloquent\Ygt\ProcessFieldRelationSelect;
use Framework\BaseClass\Http\Admin\Controller;

class ProcessFieldController extends Controller
{
    public function index()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $where              = ['company_id'=>$companyId];
        $lists              = ProcessField::getCompanyFieldList( $companyId, $where );
        $data               = [['名称','分类','类型','提示语']];
        //$typeList           = config('process.field_type_list');
        $typeList           = ProcessFieldCategory::getRows($where);
        $fieldTypeList      = config('process.process_field_type_list');
        foreach($lists as $key=>$val)
        {
            $valTitle       = $val['title'];
            $valType        = $val['type'];
            $valFieldType   = $val['field_type'];
            $placeholder    = $val['placeholder'];
            $valTypeName    = $valFieldTypeName = '';
            foreach($typeList as $k=>$v)
            {
                if($valType == $v['id'])
                {
                    $valTypeName    = $v['title'];
                }
            }
            foreach($fieldTypeList as $k=>$v)
            {
                if($valFieldType == $v['id'])
                {
                    $valFieldTypeName   = $v['title'];
                }
            }
            $temp           = [
                $valTitle,$valTypeName,$valFieldTypeName,$placeholder
            ];
            $data[]         = $temp;
        }
        return $this->exportExcel($data);
    }
    public function exportExcel($data)
    {
        \Excel::create('工序字段导出', function($excel) use($data) {
            $excel->sheet('Sheetname', function($sheet) use($data) {
                $sheet->freezeFirstRow();
                //$sheet->setAutoSize(true);
                $sheet->setWidth(array(
                    'A'=>30,'B'=>10,'C'=>30,'D'=>30
                ));
                $sheet->fromArray($data, null, 'A1', false, false);
            });
        })->export('xls');

    }
}